﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace PdfCreative.Pdf.Writer
{
    /// <summary>
    /// The PdfList class encapsulates a list of object values. It has an interface similar to other array-based
    /// lists.
    /// </summary>
    public class PdfList : PdfValue, IEnumerable<PdfValue>
    {
        // Private fields
        // ==============
        #region Private fields
        private int _finger;
        private PdfValue[] _list;
        #endregion



        // Base class overrides
        // ====================
        #region PdfValue overrides
        /// <summary>
        /// Writes the list to the specified PDF value writer.
        /// </summary>
        /// <param name="writer">The writer</param>
        internal override void Write(IPdfValueWriter writer)
        {
            writer.WriteList(this);
        }
        #endregion



        // Interface implementations
        // =========================
        #region IEnumerable<PdfValue> Members
        /// <summary>
        /// Retrieves a new enumerator for the list.
        /// </summary>
        /// <returns>The enumerator</returns>
        public IEnumerator<PdfValue> GetEnumerator()
        {
            return new PdfListEnumerator(this);
        }

        #endregion

        #region IEnumerable Members
        /// <summary>
        /// Retrieves a new enumerator for the list.
        /// </summary>
        /// <returns>The enumerator</returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new PdfListEnumerator(this);
        }

        #endregion



        // Public properties
        // =================
        #region Public properties
        /// <summary>
        /// Sets or retrieves the value at the specified index.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <returns>The value</returns>
        public PdfValue this[int index]
        {
            get
            {
                if (index >= _finger)
                    throw new ArgumentOutOfRangeException("index");

                return this.GetValue(index);
            }

            set
            {
                if (index >= _finger)
                    throw new ArgumentOutOfRangeException("index");

                _list[index] = value;
            }
        }

        /// <summary>
        /// Retrieves the number of values in the list.
        /// </summary>
        public int Count
        {
            get { return _finger; }
        }
        #endregion



        // Public methods
        // ==============
        #region Public methods
        /// <summary>
        /// Adds a new value to the list.
        /// </summary>
        /// <param name="value">The value</param>
        public void Add(PdfValue value)
        {
            if (_finger >= _list.Length)
            {
                PdfValue[] newArray = new PdfValue[_list.Length * 2];
                Array.Copy(_list, newArray, _list.Length);
                _list = newArray;
            }

            _list[_finger++] = value;
        } // Add()

        /// <summary>
        /// Adds a new string value to the list.
        /// </summary>
        /// <param name="value">The value</param>
        public void Add(string value)
        {
            this.Add(new PdfPrimitive(value));
        }

        /// <summary>
        /// Retrieves the boolean value at the specified index.
        /// </summary>
        /// <param name="index">The index</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The boolean, or the default value.</returns>
        public bool GetBooleanValue(int index, bool defaultValue)
        {
            bool result = defaultValue;

            PdfValue value = GetValue(index);
            if (null != value)
                if (false == Boolean.TryParse(value.ToString(), out result))
                    result = defaultValue;

            return result;
        } // GetBooleanValue()

        /// <summary>
        /// Retrieves the decimal value at the specified index.
        /// </summary>
        /// <param name="index">The index</param>
        /// <returns>The decimal, or the default value.</returns>
        public decimal GetDecimalValue(int index)
        {
            return GetDecimalValue(index, 0m);
        }

        /// <summary>
        /// Retrieves the decimal value at the specified index.
        /// </summary>
        /// <param name="index">The index</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The decimal, or the default value.</returns>
        public decimal GetDecimalValue(int index, decimal defaultValue)
        {
            decimal result = defaultValue;

            PdfValue value = GetValue(index);
            if (null != value)
                if (false == Decimal.TryParse(value.ToString(), out result))
                    result = defaultValue;

            return result;
        } // GetDecimalValue()

        /// <summary>
        /// Retrieves the dictionary value at the specified index.
        /// </summary>
        /// <param name="index">The index</param>
        /// <returns>The dictionary</returns>
        public PdfDictionary GetDictionary(int index)
        {
            return GetValue(index) as PdfDictionary;
        }

        /// <summary>
        /// Retrieves the integer value at the specified index.
        /// </summary>
        /// <param name="index">The index</param>
        /// <returns>The integer, or the default value.</returns>
        public int GetInt32Value(int index)
        {
            return GetInt32Value(index, 0);
        }

        /// <summary>
        /// Retrieves the integer value at the specified index.
        /// </summary>
        /// <param name="index">The index</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The integer, or the default value.</returns>
        public int GetInt32Value(int index, int defaultValue)
        {
            int result = defaultValue;

            PdfValue value = GetValue(index);
            if (null != value)
                if (false == Int32.TryParse(value.ToString(), out result))
                    result = defaultValue;

            return result;
        } // GetInt32Value()

        /// <summary>
        /// Retrieves the list value at the specified index.
        /// </summary>
        /// <param name="index">The index</param>
        /// <returns>The list</returns>
        public PdfList GetList(int index)
        {
            return GetValue(index) as PdfList;
        }

        /// <summary>
        /// Retrieves the long integer value at the specified index.
        /// If the value at the index cannot be converted to a long integer,
        /// this method returns 0.
        /// </summary>
        /// <param name="index">The index</param>
        /// <returns>The long integer</returns>
        public long GetLongValue(int index)
        {
            return GetLongValue(index, 0L);
        }

        /// <summary>
        /// Retrieves the long integer value at the specified index.
        /// If the value at the index cannot be converted to a long integer,
        /// this method returns the default value.
        /// </summary>
        /// <param name="index">The index</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The long integer, or the default value.</returns>
        public long GetLongValue(int index, long defaultValue)
        {
            long result = defaultValue;

            PdfValue value = GetValue(index);
            if (null != value)
                if (false == Int64.TryParse(value.ToString(), out result))
                    result = defaultValue;

            return result;
        } // GetlongValue()

        /// <summary>
        /// Retrieves the object reference value at the specified index. 
        /// </summary>
        /// <param name="index">The index</param>
        /// <returns>The object reference</returns>
        public PdfObjectReference GetObjectReference(int index)
        {
            return GetValue(index) as PdfObjectReference;
        }

        /// <summary>
        /// Retrieves the value at the specified index.
        /// </summary>
        /// <param name="index">The index</param>
        /// <returns>The value at that inex.</returns>
        public PdfValue GetValue(int index)
        {
            if (index >= _finger)
                throw new ArgumentOutOfRangeException("index");

            return _list[index];
        }

        /// <summary>
        /// Retrieves the value at the specified index.
        /// </summary>
        /// <param name="index">The index</param>
        /// <returns>The value at that inex.</returns>
        public T GetValue<T>(int index) where T : PdfValue
        {
            T result = default(T);

            PdfValue value = GetValue(index);
            if (null != value)
                result = (T)value;

            return result;
        }
        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new PdfList instance.
        /// </summary>
        public PdfList()
        {
            _list = new PdfValue[8];
        }
        #endregion



        // Private types
        // =============
        #region PdfListEnumerator
        /// <summary>
        /// The PdfListEnumerator class is a custom enumerator for the PdfList class. It
        /// implements the IEnumerator&lt;PdfValue&gt; interface.
        /// </summary>
        private class PdfListEnumerator : IEnumerator<PdfValue>
        {
            // Private fields
            // ==============
            #region Private fields
            private PdfList _list;
            private int _current = -1;
            #endregion



            // Interface implementations
            // =========================
            #region IEnumerator<PdfValue> Members
            /// <summary>
            /// Retrieves the current value.
            /// </summary>
            public PdfValue Current
            {
                get { return _list[_current]; }
            }
            #endregion

            #region IDisposable Members
            /// <summary>
            /// Disposes the current instance. A PdfListEnumerator instance does
            /// not perform any logic because it does not need disposing.
            /// </summary>
            public void Dispose()
            {
            }

            #endregion

            #region IEnumerator Members
            /// <summary>
            /// Retrieves the current value.
            /// </summary>
            object System.Collections.IEnumerator.Current
            {
                get { return _list[_current]; }
            }

            /// <summary>
            /// Moves to the next item in the collection and returns the validity of the enumerator.
            /// If the enumerator is no longer valid (it has moved beyond the collection) after
            /// moving to the next element, this method returns False. Otherwise, it returns True.
            /// </summary>
            /// <returns>True if the next state is valid. False if it isn't (the enumerator has reached
            /// the end of the collection).</returns>
            public bool MoveNext()
            {
                return (++_current < _list._finger);
            }

            /// <summary>
            /// Resets the enumerator.
            /// </summary>
            public void Reset()
            {
                _current = -1;
            }
            #endregion



            // Public constructor
            // ==================
            #region Public constructor
            /// <summary>
            /// Constructs a new PdfListEnumerator instance.
            /// </summary>
            /// <param name="list"></param>
            public PdfListEnumerator(PdfList list) { _list = list; }
            #endregion

        } // PdfListEnumerator class
        #endregion

    } // PdfList()
}
